import { useSafeState } from 'ahooks';
import { useEffect } from "react"
import { getWelfareSysUser } from "@/request/common"
import { loadOrgList, loadUserGroup } from "@/request/welfare"


// 查询福利管理员
export const getWelfareSysUserHook = () => {
  const [ options, setOptions ] = useSafeState<Array<Record<string,unknown>>>([])
  const getData = async () => {
    const { code, data } = await getWelfareSysUser({limit: 10000, page: 1} as any)
    if ( code === 200 ) {
      setOptions( data.list?.map((item: { userName: any; id: any })=>{
        return {
          ...item,
          label:item.userName,
          value:item.id
        }
      }) )
    } else {
      setOptions( [] )
    }
  }
  useEffect( () => {
    getData()
  }, [] )
  return options
}

// 查询上级机构
export const getOrgList = () => {
  const [ options, setOptions ] = useSafeState<Array<Record<string,unknown>>>([])
  const getData = async () => {
    const { code, data } = await loadOrgList({limit: 10000, page: 1} as any)
    if ( code === 200 ) {
      setOptions( data.list?.map((item: { orgName: any; id: any })=>{
        return {
          ...item,
          label:item.orgName,
          value:item.id
        }
      }) )
    } else {
      setOptions( [] )
    }
  }
  useEffect( () => {
    getData()
  }, [] )
  return options
}

// 查询分组
export const getGroupList = () => {
  const [ options, setOptions ] = useSafeState<Array<Record<string,unknown>>>([])
  const getData = async () => {
    const { code, data } = await loadUserGroup({limit: 10000, page: 1} as any)
    if ( code === 200 ) {
      setOptions( data.list?.map((item: { name: any; id: any })=>{
        return {
          ...item,
          label:item.name,
          value:item.id
        }
      }) )
    } else {
      setOptions( [] )
    }
  }
  useEffect( () => {
    getData()
  }, [] )
  return options
}